using Brouteforce.NewManagers;
using Brouteforce.UserInterface.Abstracts;
using Brouteforce.UserInterface.UIControls;
using Brouteforce.utilities;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Brouteforce.Screens.CreateNewCharacterScreen
{
    public class CreateNewCharacterPanel : AbstractPanel
    {
        public Slider LifeSlider;
        public Slider LightSlider;
        public Slider SpeedSlider;
        public Slider PowerSlider;

        public CreateNewCharacterPanel()
            : base("CreateNewCharacterPanel")
        {
            //var MenuItemName = "Player\n"+"number";
            //var diffBetweenLines = Draws.CalculateWordSizeNormalFont(MenuItemName);
            //var screenBounds = CalculteCenteredRectangle(panelRectangle, (int)diffBetweenLines.X, (int)Draws.NormalFontSize.Y * 2);
            //Label abstractMenuItem = new Label(MenuItemName, new Rectangle(screenBounds.X, screenBounds.Y, (int)diffBetweenLines.X, (int)diffBetweenLines.Y));
            //AddUIControl(abstractMenuItem);

            /*
            var screenBounds = panelRectangle;
            var Name = "Shooter";
            var img = GameLoop.TileShooter;
            var imgSuroundingRectangle = img.Bounds;
            Image abstractMenuItem = new Image(img, Name, new Rectangle(screenBounds.X, screenBounds.Y,imgSuroundingRectangle.Width,imgSuroundingRectangle.Height));
            AddUIControl(abstractMenuItem);
             * */
        }

        public static void LoadContent(ContentManager content)
        {
        }


        public override void Update(GameTime gameTime)
        {
            foreach (AbstractUIControl uiControl in Children)
            {
                uiControl.Update();
            }
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.End();
            spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.Opaque, SamplerState.LinearWrap,
                              DepthStencilState.Default, RasterizerState.CullNone);
            spriteBatch.Draw(UIControlManager.Instance.BlackStoneTexture, new Vector2(Bounds.X, Bounds.Y), Bounds,
                             Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
            spriteBatch.End();
            spriteBatch.Begin();
            Draws.DrawBorder(Bounds, new Color(91, 91, 91), 2);
            foreach (AbstractUIControl uiControl in Children)
            {
                uiControl.Draw(gameTime);
            }
        }


        public override void AfterLayout()
        {
            var image = UIControlManager.Instance.CreateButtonTexture;
            Button abstractMenuItem = new Button(image, "Create Button",
                                                 new Rectangle((Bounds.Width - image.Width)/2,
                                                               (Bounds.Height - image.Height) - 10, image.Width,
                                                               image.Height));
            abstractMenuItem.setOffset(new Vector2(Bounds.X, Bounds.Y));
            abstractMenuItem.OnClick += (() => ScreenManager.Instance.CloseNewCharacterScreen(this));
            AddUIControl(abstractMenuItem);


            //create "Resources Status :" label
            var labelText = "Life :";
            var diffBetweenLines = Draws.CalculateWordSizeNormalFont(labelText);

            //***********************************************************************calculating the middle of the panel - taking account of all the following UI controls
            var spaceBetweenUIControls = 10;
            Rectangle centerLocation = CalculteCenteredRectangle(Bounds,
                                                                 (UIControlManager.Instance.SliderBackgroundTexture.Bounds.
                                                                      Width + spaceBetweenUIControls +
                                                                  (int) diffBetweenLines.X),
                                                                 ((int)
                                                                  (MathHelper.Max(
                                                                      UIControlManager.Instance.SliderBackgroundTexture.Bounds.
                                                                          Height, diffBetweenLines.Y))*4) +
                                                                 spaceBetweenUIControls*4);
            var XPostionSliders = centerLocation.X + spaceBetweenUIControls + (int) diffBetweenLines.X;
            var YDiffBetweenControls =
                (int) (MathHelper.Max(UIControlManager.Instance.SliderBackgroundTexture.Bounds.Height, diffBetweenLines.Y)) +
                spaceBetweenUIControls;

            //************************************************************************

            //labelText = "Life :" + it's slider
            Label label = new Label(labelText, labelText,
                                    new Rectangle(centerLocation.X, centerLocation.Y, (int) diffBetweenLines.X,
                                                  (int) diffBetweenLines.Y));
            label.setOffset(new Vector2(Bounds.X, Bounds.Y));
            AddUIControl(label);


            
            LifeSlider= new Slider(UIControlManager.Instance.SliderBackgroundTexture,
                                       UIControlManager.Instance.SliderScrollBarTexture,
                                       "Life Slider",
                                       new Rectangle(XPostionSliders, centerLocation.Y,
                                                     UIControlManager.Instance.SliderBackgroundTexture.Width,
                                                     UIControlManager.Instance.SliderBackgroundTexture.Height),
                                       0, 20, Color.Brown);
            LifeSlider.setOffset(new Vector2(Bounds.X, Bounds.Y));
            AddUIControl(LifeSlider);

            //labelText = "Life :" + it's slider
            labelText = "Light :";
            label = new Label(labelText, labelText,
                              new Rectangle(centerLocation.X, centerLocation.Y + YDiffBetweenControls,
                                            (int) diffBetweenLines.X,
                                            (int) diffBetweenLines.Y));
            label.setOffset(new Vector2(Bounds.X, Bounds.Y));
            AddUIControl(label);


            LightSlider = new Slider(UIControlManager.Instance.SliderBackgroundTexture,
                                UIControlManager.Instance.SliderScrollBarTexture,
                                "Light Slider",
                                new Rectangle(XPostionSliders,
                                              centerLocation.Y + YDiffBetweenControls,
                                              UIControlManager.Instance.SliderBackgroundTexture.Width,
                                              UIControlManager.Instance.SliderBackgroundTexture.Height),
                                0, 20, Color.Brown);
            LightSlider.setOffset(new Vector2(Bounds.X, Bounds.Y));
            AddUIControl(LightSlider);



            //labelText = "Speed :" + it's slider
            labelText = "Speed :";
            label = new Label(labelText, labelText,
                              new Rectangle(centerLocation.X, centerLocation.Y + YDiffBetweenControls*2,
                                            (int)diffBetweenLines.X,
                                            (int)diffBetweenLines.Y));
            label.setOffset(new Vector2(Bounds.X, Bounds.Y));
            AddUIControl(label);


            SpeedSlider = new Slider(UIControlManager.Instance.SliderBackgroundTexture,
                                UIControlManager.Instance.SliderScrollBarTexture,
                                "Speed Slider",
                                new Rectangle(XPostionSliders,
                                              centerLocation.Y + YDiffBetweenControls * 2,
                                              UIControlManager.Instance.SliderBackgroundTexture.Width,
                                              UIControlManager.Instance.SliderBackgroundTexture.Height),
                                0, 20, Color.Brown);
            SpeedSlider.setOffset(new Vector2(Bounds.X, Bounds.Y));
            AddUIControl(SpeedSlider);






            //labelText = "Life :" + it's slider
            labelText = "Power :";
            label = new Label(labelText, labelText,
                              new Rectangle(centerLocation.X, centerLocation.Y + YDiffBetweenControls * 3,
                                            (int)diffBetweenLines.X,
                                            (int)diffBetweenLines.Y));
            label.setOffset(new Vector2(Bounds.X, Bounds.Y));
            AddUIControl(label);


            PowerSlider = new Slider(UIControlManager.Instance.SliderBackgroundTexture,
                                UIControlManager.Instance.SliderScrollBarTexture,
                                "Power Slider",
                                new Rectangle(XPostionSliders,
                                              centerLocation.Y + YDiffBetweenControls * 3,
                                              UIControlManager.Instance.SliderBackgroundTexture.Width,
                                              UIControlManager.Instance.SliderBackgroundTexture.Height),
                                0, 20, Color.Brown);
            PowerSlider.setOffset(new Vector2(Bounds.X, Bounds.Y));
            AddUIControl(PowerSlider);

        }
    }
}

